<template>
  <el-dialog :visible="isApply" :before-close="cancel" append-to-body>
    <template slot="title">
      {{title}}
    </template>

    <el-form :model="applyForm" :rules="ruleApply" ref="applyForm" label-width="120px">
      <el-form-item label="申请修改原因" prop="remark">
        <el-input type="textarea" :rows="4" v-model="applyForm.remark"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submit('applyForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>

  import {delMemberBankcard,updateMemberInfo,updateMemberBankcard} from '@/services/api/member'
  import {resetForm} from '@/build'
  export default {
    name: "apply-dialog",
    props: ['value', 'rowData'],
    mixins: [resetForm],
    data(){
      return {
        applyForm:{
        },
        visible: false,
        ruleApply: {
          remark:[
            { required: true, message: '请填写申请原因', trigger: 'blur' },
          ]
        }
      }
    },
    computed:{
      isApply(){
        return this.$store.state.member.isApply
      },
      title(){
        let {from} = this.$store.state.member.applyData;
        switch (from) {
          case 'memberInfo':
            return '申请修改资料';
            break;
          case 'resetName':
            return '申请重置姓名和银行卡';
            break;
          case 'delCard':
            return '申请删除银行卡';
            break;
        }
      }
    },
    methods: {
      cancel() {
        this.$store.commit(types.HIDE_APPLY_MODAL);
        this.resetForm('applyForm')
      },
      submit(formName) {
        let {data,from} = this.$store.state.member.applyData;
        data = {...data,...this.applyForm}
        this.$refs[formName].validate((valid) => {
          if (valid) {
            switch (from) {
              case 'memberInfo':
                this._updateInfo(data);
                break;
                break;
              case 'resetName':
                this._updateBankcard(data);
                break;
              case 'delCard':
                this._delBankCard(data);
                break;
            }
            this.$store.commit(types.HIDE_APPLY_MODAL);
            this.resetForm('applyForm')
          } else {
            return false;
          }
        });
      },
      _updateInfo(data){
        updateMemberInfo(data).then(([result,data])=>{
          if(result){
            this.$notify({
              type:'success',
              message: '账户编辑申请成功，等待审核中...'
            })
          }else{
            this.$notify({
              type:'error',
              message: data.message
            })
          }
        })
      },
      _delBankCard(data){
        delMemberBankcard(data).then(([result,data])=>{
          if(result){
            this.$notify({
              type:'success',
              message: '删除银行卡申请成功，等待审核中...'
            })
          }else{
            this.$notify({
              type:'error',
              message: data.message
            })
          }
        })
      },
      _updateBankcard(data){
        updateMemberBankcard(data).then(([result,data])=>{
          if(result){
            this.$notify({
              type:'success',
              message: '银行卡重置成功，等待审核中...'
            })
          }else{
            this.$notify({
              type:'error',
              message: data.message
            })
          }
        })
      },
    }
  }
</script>

<style scoped>

</style>
